package mlp.game.applebucking;

import com.overkill.game.RenderView;
import com.overkill.game.Sprite;
import com.overkill.game.SpriteRenderable;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.Log;

public class Apple extends SpriteRenderable {
	public enum Type{ GOOD, ROTTEN, POWER }
	
	public static int MOVEMENTSPEED_PX_MIN = 4;
	public static int MOVEMENTSPEED_PX_MAX = 9;
	
	private int movementSpeedPx = 2;
	private int movementDelayMs = 10;
	private long lastTimeMoved = 0;
	
	private float angle = -30;
	private int clockwise = 1; //1.. clockwise; -1.. counterclockwise
	
	private Type type = Type.GOOD;
	
	public Apple(Sprite sprite, int startX, int startY, int speed, int clockwise){
		super(sprite);
		moveTo(startX, startY);
		this.movementSpeedPx = speed;
		this.clockwise = clockwise;
		angle = AppleBloomEngine.random.nextInt(360);
		if(RenderView.SHOW_DEBUG_INFO){
			Log.d("Apple", "Apple is turning ("+clockwise+")");
		}
	}
	
	public Apple(Sprite sprite, int startX, int startY, int speed){
		super(sprite);
		moveTo(startX, startY);
		this.movementSpeedPx = speed;
		this.useRandomDirection();
	}
		
	public Apple(Sprite sprite, int startX, int startY){
		super(sprite);
		moveTo(startX, startY);
		this.useRandomMovementSpeed();
		this.useRandomDirection();
	}
		
	public Apple(Sprite sprite, int maxX, Type type){
		this(sprite, maxX, 0, type);
	}
	
	public Apple(Sprite sprite, int maxX, int offsetY, Type type){
		super(sprite);
		moveTo(AppleBloomEngine.random.nextInt(maxX - super.getSprite().getWidth()), -super.getSprite().getHeight() + offsetY);		
		this.useRandomMovementSpeed();
		this.useRandomDirection();
		this.type = type;
	}
	
	public Apple(Sprite sprite, int maxX, int offsetY, Type type, int speed){
		super(sprite);
		moveTo(AppleBloomEngine.random.nextInt(maxX - super.getSprite().getWidth()), -super.getSprite().getHeight() + offsetY);		
		this.movementSpeedPx = speed;
		this.useRandomDirection();
		this.type = type;
	}
	
	private void useRandomMovementSpeed(){
		this.movementSpeedPx = AppleBloomEngine.random.nextInt((Apple.MOVEMENTSPEED_PX_MAX - Apple.MOVEMENTSPEED_PX_MIN)) + Apple.MOVEMENTSPEED_PX_MIN;
	}
	
	private void useRandomDirection(){
		clockwise = AppleBloomEngine.random.nextBoolean() ? 1 : -1;
		angle = AppleBloomEngine.random.nextInt(360);
		if(RenderView.SHOW_DEBUG_INFO){
			Log.d("Apple", "Apple is turning ("+clockwise+")");
		}
	}
	
	@Override
	public void update(long gameTime){
		if(gameTime - lastTimeMoved > movementDelayMs){
			super.moveBy(0, movementSpeedPx);
			lastTimeMoved = gameTime;
		}
		super.update(gameTime);
	}
	
	@Override
	public void draw(Canvas canvas){
		Sprite sprite = getSprite();
		PointF position = getPosition();
		angle += (movementSpeedPx/2)*clockwise;
		if(angle >= 360){
			angle -= 360;
		} else if (angle <= -360){
			angle +=360;
		}
		Matrix matrix = canvas.getMatrix();
		canvas.rotate(angle, position.x + sprite.getWidth()/2, position.y + sprite.getHeight()/2);
		sprite.drawAt(position, canvas);
		canvas.setMatrix(matrix);
		
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Type getType() {
		return type;
	}
}
